﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.BLL;
using Ajax;
using SeDuce.Model;
using System.Data;
using SeDuce.Utility;

public partial class Contact : System.Web.UI.Page
{
    Lazy_Yu<BrandBLL> lazyBrandBLL = new Lazy_Yu<BrandBLL>(() => new BrandBLL());
    public BrandBLL LazyBrandBLL
    {
        get { return this.lazyBrandBLL.Value; }
    }

    Lazy_Yu<ArticleBLL> lazyArticleBLL = new Lazy_Yu<ArticleBLL>(() => new ArticleBLL());
    public ArticleBLL LazyArticleBLL
    {
        get { return this.lazyArticleBLL.Value; }
    }

    Lazy_Yu<AdBLL> lazyAdBLL = new Lazy_Yu<AdBLL>(() => new AdBLL());
    public AdBLL LazyAdBLL
    {
        get { return this.lazyAdBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Contact));
        BindCategories();

        List<ArticleCategory> titles = new List<ArticleCategory>();
        List<SeDuce.Model.Article> articles = this.LazyArticleBLL.GetArticleByType((int)(Misc.ArticleTypeEnum)Enum.Parse(typeof(Misc.ArticleTypeEnum), "联系SeDuce", true)).ToList<SeDuce.Model.Article>();
        foreach (SeDuce.Model.Article a in articles)
        {
            titles.Add(new ArticleCategory() { ID = a.ID, TitleText = a.Title });
        }

        if (!IsPostBack)
        {
            this.rptAds.DataSource = this.LazyAdBLL.GetAds().ToList<Ad>().Where<Ad>(a => a.Enabled == true);
            this.rptAds.DataBind();
            if (titles.Count > 0)
                SetContent(titles[0].ID);
        }
        this.Repeater1.DataSource = titles;
        this.Repeater1.DataBind();
    }


    protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "CATEGORY_CHANGE")
        {
            LinkButton lbtnCategory = (LinkButton)e.Item.FindControl("lbtnCategory");
            Response.Redirect(ResolveUrl("~/Brands/" + e.CommandArgument.ToString()));
        }
    }

    private void BindCategories()
    {
        this.rptCategories.DataSource = this.LazyBrandBLL.GetBrands().ToList<Brand>().OrderBy(t => t.Sort).ToList<Brand>();
        this.rptCategories.DataBind();
    }

    [Ajax.AjaxMethod()]
    public bool UpdateBrands()
    {
        try
        {
            BindCategories();
            return true;
        }
        catch { return false; }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Title_Change")
        {
            try
            {
                LinkButton lbtnTitle = (LinkButton)e.Item.FindControl("lbtnTitle");
                lbtnTitle.ForeColor = System.Drawing.Color.FromArgb(197, 147, 16);
                SetContent(e.CommandArgument.ToString().ToInt());
            }
            catch { }
        }
    }

    private void SetContent(int id)
    {
        DataTable dtArticle = LazyArticleBLL.GetArticleByID(id);
        if (dtArticle.Rows.Count > 0)
        {
            this.literContent.Text = dtArticle.Rows[0]["ContentText"].ToString();
        }
    }
}